package com.meaningful.design_pattern.strategy_pattern;

import cn.hutool.extra.spring.SpringUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;

import java.util.function.LongPredicate;

@Service
public class CustomService {

    @PostConstruct
    public void initService() {
        System.out.println("客服服务加载完毕......");
    }

    public enum CustomerType {
        POOR_GUY(
                level -> level < 30_000L,
                SpringUtil.getBean("defaultCustomServiceStrategy",TencentCustomServiceStrategy.class)
        ),
        V1(
                level -> level >= 30_000L && level < 60_000L,
                SpringUtil.getBean("v1CustomServiceStrategy",TencentCustomServiceStrategy.class)
        ),
        V2(
                level -> level >=60_000L && level <400_000L,
                SpringUtil.getBean("v2CustomServiceStrategy",TencentCustomServiceStrategy.class)
        ),
        V3(
                level -> level >= 400_000L && level < 800_000L,
                SpringUtil.getBean("v3CustomServiceStrategy",TencentCustomServiceStrategy.class)
        ),
        V4(
                level -> level >= 800_000L && level < 2_000_000L,
                SpringUtil.getBean("v4CustomServiceStrategy",TencentCustomServiceStrategy.class)
        ),
        V5(
                level -> level >= 2_000_000L,
                SpringUtil.getBean("v5CustomServiceStrategy",TencentCustomServiceStrategy.class)
        );

        private final LongPredicate support;

        private final TencentCustomServiceStrategy customServiceStrategy;

        CustomerType(LongPredicate support, TencentCustomServiceStrategy customServiceStrategy) {
            this.support = support;
            this.customServiceStrategy = customServiceStrategy;
        }

        public static CustomerType support(long level) {
            for (CustomerType customerType : values()) {
                if (customerType.support.test(level)) {
                    return customerType;
                }
            }
            throw new RuntimeException("不支持的客户等级：" + level);
        }

        public TencentCustomServiceStrategy getCustomServiceStrategy() {
            return customServiceStrategy;
        }

    }

    public String getService(long level) {
        TencentCustomServiceStrategy customServiceStrategy = CustomerType.support(level).getCustomServiceStrategy();
        return customServiceStrategy.findCustomService();
    }

}
